/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agenda.business;

import agenda.conector.Conector;
import agenda.model.MdAgenda;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Julien
 */
public class AgendaBO extends Conector {

    public AgendaBO() {
    }

    public boolean incluir(MdAgenda agenda) {
        try {

            int val = getConnection().createStatement().executeUpdate("INSERT INTO agenda.agenda"
                    + "(nome,"
                    + "Sobrenome,"
                    + "telefoneFixo,"
                    + "telefoneMovel)"
                    + "VALUES"
                    + "(\"" + agenda.getNome() + "\","
                    + "\"" + agenda.getSobrenome() + "\","
                    + "\"" + agenda.getTelefoneFixo() + "\","
                    + "\"" + agenda.getTelefoneMovel() + "\"); ");
        } catch (Exception ex) {
            Logger.getLogger(AgendaBO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return true;


    }

    public boolean alterar(MdAgenda agenda) {
        try {

            int val = getConnection().createStatement().executeUpdate("UPDATE agenda.agenda SET"
                    + "nome = \"" + agenda + "\","
                    + "Sobrenome = \"" + agenda + "\","
                    + "telefoneFixo = \"" + agenda + "\","
                    + "telefoneMovel = \"" + agenda + "\","
                    + "WHERE"
                    + "codigo = " + agenda.getNome());
        } catch (Exception ex) {
            Logger.getLogger(AgendaBO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return true;

    }

    public List<MdAgenda> listarTodos() {

        List<MdAgenda> agendas = new ArrayList<MdAgenda>();

        try {

            ResultSet _result = getConnection().createStatement().executeQuery("SELECT * FROM agenda.agenda");

            while (_result.next()) {
                MdAgenda agenda = new MdAgenda();

                agenda.setCodigo(_result.getInt("codigo"));
                agenda.setNome(_result.getString("nome"));
                agenda.setSobrenome(_result.getString("Sobrenome"));
                agenda.setTelefoneFixo(_result.getString("telefoneFixo"));
                agenda.setTelefoneMovel(_result.getString("telefoneMovel"));

                agendas.add(agenda);
            }

        } catch (Exception ex) {
            Logger.getLogger(AgendaBO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return agendas;

    }
    
    public boolean excluir (MdAgenda agenda){
        
        try {

            int val = getConnection().createStatement().executeUpdate("DELETE FROM agenda.agenda WHERE agenda.codigo="+agenda.getCodigo());
        } catch (Exception ex) {
            Logger.getLogger(AgendaBO.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        
        return true;
    }
}
